<?php
namespace Little\Controller;

use Think\Controller;

header("Content-type: text/html; charset=utf-8");

//********************订单信息************************

class OrdersController extends Controller
{
    //covers_shu表数据规则
    //`nums_one` '签到积分数',
    //`nums_two` '消费积分数',
    //`nums_three` '分享积分数',
    //`nums_four`'送花兑换积分数',
    //`nums_five` '兑换礼品积分数',
    //`nums_six` '一朵花的价格',
    //`nums_seven` '1积分值多少钱',
    //`nums_eight` '用户消费成功后，转介者每一元钱可获得的积分收益',
    //商品 购物车支付
    public function indexs(){
        $type   = I('type');  // 1购买一件产品 2购物车里批量购买
        $status = I('status');//1支付成功   2待付款
        $openid = I('op');
        $orders_id = I('orders_id'); //判断用户是否是从待付款那里跳转过来的
        if ($orders_id != "undefined" && $orders_id) {
            //删除该待付款订单
            $ord = del("orders", "id = {$orders_id}");
        }
        $data['trade_no'] = I('no');   //订单编号

        $users = sel("users", "id,covers,coupons_id", "openid like '{$openid}'");
        //销毁用户选择优惠券的缓存
        $up = up_one("users", "id = {$users['id']}", array("coupons_id" => 0));
        if ($status == 1) {
            //积分
            $data['covers'] = I('covers');
            //使用优惠券
            $you = I('you_id');//优惠券id
            $you_jian = I('you_jian');
            if ($you && $you_jian) {
                $data['coupons_id'] = $you;
                $data['coupons_jian'] = $you_jian;
            }
            //优惠券状态值改变
            if ($you) {
                $yous = sel("users_coupons", "id", "user_id = {$users['id']} and coupons_id = {$you}");
            } else {
                $yous = sel("users_coupons", "id", "user_id = {$users['id']} and coupons_id = {$users['coupons_id']}");
            }
            if ($yous) {
                $up_you = up_one("users_coupons", "id = {$yous['id']}", array("type" => 2));
            }
        }

        $data['user_id']  = $users['id'];
        $data['address']  = I('address');//收货地址
        $data['name']     = I('name');
        $data['phone']    = I('phone');
        $data['create_at'] = time();
        $table = "orders";
        if($status == 1){
            //消费可获积分情况
            $covers_shu  = sel("covers_shu","nums_two,nums_eight");
            //是否有转介者
            $zhuan_user  = sel("zhuan","id,user_id,status","zhuan_id = {$users['id']}");
            if(!$zhuan_user){$zhuan_user = 1;}
        }
        if ($type == 1) {  //一种产品的支付
            $filed  = "lei,moneys";
            $g_table = "goods";

            $data['goods_id'] = I('id');     //商品id
            $xing    = I('xing');
            $store   = sel($g_table, $filed, "id = {$data['goods_id']}");
            if ($store['lei'] != "") {
                //订单类型的价格
                if (!$xing) {
                    $lei  = explode(",",$store['lei']);
                    $xin  = explode("/",$lei[0]);
                    $xing = $xin[1];
                }
                $data['xing'] = $xing;
            } else {
                $data['xing'] = "暂无";
            }
            $data['stores_id'] = $store['stores_id'];
            $data['nums'] = I('shop_num');  //商品数量
            $data['moneys'] = I('moneys');  //商品总价
            if ($status == 1) {
                //商品库存销量变动
                $bian = sel($g_table, "xiao,nums", "id = {$data['goods_id']}");
                $x = $bian['xiao'] + 1;
                $n = $bian['nums'] - 1;
                $x_n = up_one($g_table, "id = {$data['goods_id']}", array("xiao" => $x, "nums" => $n));
                $data['status'] = 3;//默认待收货
                //积分获得
                $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],1,8);
                //用户使用积分减少
                if ($data['covers'] > 0) {
                    $c = $users['covers'] - $data['covers'];
                    $u = up_one("users", "id = {$users['id']}", array("covers" => $c));//用户积分减少
                    $cs = "-" . $data['covers'];
                    $co = ins("users_covers", array("user_id" => $users['id'], "shi_id" => $data['goods_id'],"type"=>1, "covers" => $cs, "create_at" => time()));
                }
                $res = ins($table, $data);
            }
            if ($status == 2) {
                $data['status'] = 1;
                $res = ins($table, $data);
            }
        }
        /*if ($type == 2) {
            $list = json_decode($_GET['cars'], true);
            foreach ($list as $k => $v) {
                $data['nums'] = $v['shop_num'];
                $cars_id = $v['id'];
                $data['type'] = $v['type'];

                $filed   = "id,lei,moneys";
                $g_table = "goods";

                $data['moneys'] = $v['shop_money'];
                $store = sel($g_table, $filed, "id = {$v['goods_id']}");
                $data['goods_id'] = $store['id'];
                if ($store['lei'] != "") {
                    //订单类型的价格
                    if (!$v['xing']) {
                        $lei  = explode(",",$store['lei']);
                        $xin  = explode("/",$lei[0]);
                        $xing = $xin[1];
                    }
                    $data['xing'] = $xing;
                } else {
                    $data['xing'] = "暂无";
                }
                if ($status == 1) {
                    //商品库存销量变动
                    $bian = sel($g_table, "xiao,nums", "id = {$data['goods_id']}");
                    $x = $bian['xiao'] + 1;
                    $n = $bian['nums'] - 1;
                    $x_n = up_one($g_table, "id = {$data['goods_id']}", array("xiao" => $x, "nums" => $n));

                    $data['status'] = 2;
                    $res = ins($table, $data);
                }
                if ($status == 2) {
                    $data['status'] = 1;
                    $res = ins($table, $data);
                }
                if ($res) {  ///删除购物车中得产品
                    $c_del = del("cars", "id = {$cars_id}");
                }
            }
            if ($status == 1) {  //支付成功 用户积分减少
                if ($data['covers'] > 0) {
                    $c = $users['covers'] - $data['covers'];
                    $u = up_one("users", "id = {$users['id']}", array("covers" => $c));//用户积分减少
                    $cs = "-" . $data['covers'];
                    $co = ins("users_covers", array("user_id" => $users['id'],"type"=>1, "shi_id" => $data['goods_id'],"covers" => $cs, "create_at" => time()));//积分明细
                }
            }
        }*/
        if ($type == 2) {
            $list = json_decode($_GET['cars'], true);
            foreach ($list as $k => $v) {
                $data['nums'] = $v['shop_num'];
                $cars_id = $v['id'];
                $data['moneys'] = $v['shop_money'];
                $m[] =  $v['shop_money'];
                $store = sel("goods", "id,lei,moneys", "id = {$v['goods_id']}");
                $data['goods_id'] = $store['id'];
                if ($store['lei'] != "") {
                    //订单类型的价格
                    $lei = explode(",", $store['lei']);
                    $a   = explode("/", $lei[0]);
                    if (!$v['xing']) {
                        $xing = $a[1];
                    } else {
                        $xing = $v['xing'];
                    }
                    $data['xing'] = $xing;
                } else {
                    $data['xing'] = "暂无";
                }
                $data['stores_id'] = $store['stores_id'];

                if ($status == 1) {
                    //积分获得
                    $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],1,8);
                    //商品库存销量变动
                    $bian = sel("goods", "xiao,nums", "id = {$data['goods_id']}");
                    $x = $bian['xiao'] + 1;
                    $n = $bian['nums'] - 1;
                    $x_n = up_one("goods", "id = {$data['goods_id']}", array("xiao" => $x, "nums" => $n));

                    $data['status'] = 3;
                    $res = ins("orders", $data);
                }
                if ($status == 2) {
                    $data['status'] = 1;
                    $res = ins("orders", $data);
                }
                if ($res) {  ///删除购物车中得产品
                    $c_del = del("cars", "id = {$cars_id}");
                }
            }
            if ($status == 1) {  //支付成功 用户积分减少
                if ($data['covers'] > 0) {
                    $c = $users['covers'] - $data['covers'];
                    $u = up_one("users", "id = {$users['id']}", array("covers" => $c));//用户积分减少
                    $cs = "-" . $data['covers'];
                    $co = ins("users_covers", array("user_id" => $users['id'], "shi_id" => $data['goods_id'], "type" => 1, "covers" => $cs, "create_at" => time()));//积分明细
                }
            }
        }
    }
    //线上报名支付
    public function line(){
        $status            = I('status');//1支付成功   2支付失败
        if ($status == 1) {
            $openid            = I('op');
            $data['trade_no'] = I('no');   //订单编号
            $data['moneys']   = I('moneys');  //商品总价
            $data['goods_id'] = I('id');     //商品id
            $data['type']     = 2;
            $data['status']   = 2;
            $users = sel("users", "id,covers", "openid like '{$openid}'");
            $data['user_id'] = $users['id'];
            $data['create_at'] = time();
            //存储订单
            $res = ins("orders", $data);
            if($res){
                //积分获得
                //消费可获积分情况
                $covers_shu  = sel("covers_shu","nums_two,nums_eight");
                //是否有转介者
                $zhuan_user  = sel("zhuan","id,user_id,status","zhuan_id = {$users['id']}");
                if(!$zhuan_user){$zhuan_user = 1;}
                $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],3,8);
            }
            $fan = 2;//支付成功
        }else{
            $fan = 1;//支付失败
        }
        echo json_encode(array("info"=>$fan));
    }
    //攻略课堂报名
    public function schoolroom(){
        $status            = I('status');//1支付成功   2支付失败
        if ($status == 1) {
            $openid            = I('op');
            $data['trade_no'] = I('no');   //订单编号
            $data['moneys']   = I('moneys');  //商品总价
            $data['goods_id'] = I('id');     //商品id
            $data['type']     = 3;
            $data['status']   = 2;
            $users = sel("users", "id,covers", "openid like '{$openid}'");
            $data['user_id'] = $users['id'];
            $data['create_at'] = time();
            //存储订单
            $res = ins("orders", $data);
            //消费用户获得积分
            if($res){
                //积分获得
                //消费可获积分情况
                $covers_shu  = sel("covers_shu","nums_two,nums_eight");
                //是否有转介者
                $zhuan_user  = sel("zhuan","id,user_id,status","zhuan_id = {$users['id']}");
                if(!$zhuan_user){$zhuan_user = 1;}
                $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],4,8);
            }
            $fan = 2;//支付成功
        }else{
            $fan = 1;//支付失败
        }
        echo json_encode(array("info"=>$fan));
    }
    //orders_vip VIP、购买
    public function vips(){
        $status            = I('status');//1支付成功   2支付失败
        if ($status == 1) {
            $openid             = I('op');
            $data['trade_no']  = I('no');   //订单编号
            $data['moneys']    = I('moneys');  //商品总价
            $data['vips_id']   = I('id');     //商品id
            $data['nums']      = I('nums');   //用户查看次数
            $vips               = sel("vips","look_people,xian_nums,vip_type,end_date","id = {$data['vips_id']}");
            $users              = sel("users", "id,covers,cha_nums,xian_nums,end_at", "openid like '{$openid}'");
            $data['user_id']   = $users['id'];
            $data['create_at'] = time();
            //修改用户可以查看别人的次数  以及参加线下活动的次数
            $u['cha_nums'] = $users['cha_nums']+$data['nums'];
            if($vips['xian_nums'] == -1){
                $u['xian_nums'] = -1;
            }else{
                $u['xian_nums'] = $users['xian_nums']+$vips['xian_nums'];
            }
            //用户vip周期改变
            if($vips['end_date'] == -1){
                $u['end_at'] = -1;
            }else{
                if($users['end_at'] > time()){
                    $u['end_at'] = $users['end_at']+$vips['end_date']*30*24*60*60;
                }else{
                    $u['end_at'] = strtotime("+{$vips['end_date']} month");
                }
            }
            $u['vip_type'] =  $data['vips_id'];
            //用户查看次数等信息变动
            $u              = up_one("users", "id = {$users['id']}",$u);
            //存储订单
            $res = ins("orders_vip", $data);

            if($res){
                //积分获得
                //消费可获积分情况
                $covers_shu  = sel("covers_shu","nums_two,nums_eight");
                //是否有转介者
                $zhuan_user  = sel("zhuan","id,user_id,status","zhuan_id = {$users['id']}");
                if(!$zhuan_user){$zhuan_user = 1;}
                $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],5,8);
            }

            $fan = 2;//支付成功
        }else{
            $fan = 1;//支付失败
        }
        echo json_encode(array("info"=>$fan));
    }
    //用户购买送花
    public function flowers(){
        $status            = I('status');//1支付成功   2支付失败
        if ($status == 1) {
            $bei_song          = I("bei_id");//被送花用户的id
            $openid            = I('op');
            $data['trade_no'] = I('no');   //订单编号
            $data['moneys']   = I('moneys');  //商品总价
            $data['nums']     = I('nums');  //商品总价
            $data['goods_id'] = $bei_song;
            $data['type']      = 4;
            $data['status']    = 2;
            $users = sel("users", "id,covers", "openid like '{$openid}'");
            $data['user_id'] = $users['id'];
            $data['create_at'] = time();
            //存储订单
            $res = ins("orders", $data);
            //消费用户获得积分
            if($res){
                //积分获得
                //消费可获积分情况
                $covers_shu  = sel("covers_shu","nums_two,nums_eight");
                //是否有转介者
                $zhuan_user  = sel("zhuan","id,user_id,status","zhuan_id = {$users['id']}");
                if(!$zhuan_user){$zhuan_user = 1;}
                $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],6,8);
            }
            //被送花用户信息存储
            $bei_user = sel("users","flowers","id = {$bei_song}");
            $b_flowers = $bei_user['flowers']+$data['nums'];
            $b_cun     = up_one("users","id = {$bei_song}",array("flowers"=>$b_flowers));

            $fan = $b_flowers;//支付成功
        }else{
            $fan = 1;//支付失败
        }
        echo json_encode(array("info"=>$fan));
    }
    //用户首次分享获得积分
    public function fen(){
        $code = I('code');
        $user = users("users","id,fen,covers",$code);
        if($user['fen']){
            echo 1;
        }else{
            //分享可获积分情况
            $covers_shu  = sel("covers_shu","nums_three");
            $covers      = $user['covers']+$covers_shu['nums_three'];
            $cun         = up_one("users","id = {$user['id']}",array("covers"=>$covers,"fen"=>time()));
            if($cun){
                $z_cs  = "+".$covers_shu['nums_three'] ;
                $u_c_z = ins("users_covers",array("user_id"=>$user['id'],"covers"=>$z_cs,"type"=>9,"create_at"=>time()));
                echo 2;
            }else{
               echo 1;
            }
        }
    }
    // 资讯中报名支付
    public function news(){
        $status     = I('status');//1支付成功   2支付失败
        $code       = I('code');//会员免费报名
        if ($status == 1) {
            $data['goods_id'] = I('id');     //商品id
            if($code){
                $users = users("users","id,covers",$code);
            }else{
                $openid            = I('op');
                $data['trade_no'] = I('no');   //订单编号
                $data['moneys']   = I('moneys');  //商品总价
                $users = sel("users", "id,covers", "openid like '{$openid}'");
            }
            $data['type']      = 5;
            $data['status']    = 2;

            $data['user_id'] = $users['id'];
            $data['create_at'] = time();
            //存储订单
            $res = ins("orders", $data);
            if($res && $code){
                //积分获得
                //消费可获积分情况
                $covers_shu  = sel("covers_shu","nums_two,nums_eight");
                //是否有转介者
                $zhuan_user  = sel("zhuan","id,user_id,status","zhuan_id = {$users['id']}");
                if(!$zhuan_user){$zhuan_user = 1;}
                $huo = xiao_yi($covers_shu,$data['moneys'],$users,$zhuan_user,$data['goods_id'],10,8);
            }
            $fan = 2;//支付成功
        }else{
            $fan = 1;//支付失败
        }
        echo json_encode(array("info"=>$fan));
    }
}